package xs.com.test;

import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;

import xs.com.base.BaseRequestBean;
import xs.com.base.SoapBaseGsonThreadHandler;
import xs.com.bean.CarNumberBean;
import xs.com.bean.ResponeTestBean;
import xs.com.location.utils.AppTool;
import xs.com.location.utils.LocationManagerTool;
import xs.com.mylibrary.allinone.view.annotation.ViewInject;
import xs.com.mylibrary.allinone.view.annotation.event.OnClick;

public class MainActivity extends BaseActivity {
    @ViewInject(R.id.main_prompt_tv)
    TextView promptTv;
    @ViewInject(R.id.main_cars_nearby_tv)
    TextView carNearbyTv;
    @ViewInject(R.id.main_my_location_tv)
    TextView myLocationTv;
    @ViewInject(R.id.main_use_car_btn)
    Button useCarBtn;
    @ViewInject(R.id.main_where_go_tv)
    TextView goWhereTv;

    @OnClick(R.id.main_use_car_btn)
    protected void clickUserCar(View v) {
        AppTool.activiyJump(mContext, UserCarMapActivity.class);
    }

    protected LocationManagerProxy mLocationManagerProxy;
    double geoLat;
    double geoLng;

    @Override
    protected void initView() {
        super.initView();

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        initLocation();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    private void getCarNearby(final double geoLat, final double geoLng) {
        myLocationTv.setTextColor(getResources().getColor(R.color.blue));
        SoapBaseGsonThreadHandler<CarNumberBean> threadhandler = new SoapBaseGsonThreadHandler<CarNumberBean>(mContext) {
            @Override
            protected BaseRequestBean getBaseRequestBean() {
                HashMap<String, String> data = new HashMap<String, String>();
                data.put("Lat", geoLat + "");
                data.put("Lng", geoLng + "");
                data.put("Radius", 100 + "");
                data.put("ReturnType", "0");
                data.put("Type", "0");
                return new BaseRequestBean("GetNearCar", data, new TypeToken<ResponeTestBean<CarNumberBean>>() {
                }.getType());
            }

            @Override
            public void successEndT(CarNumberBean obj) {
                String prompt = getResources().getString(R.string.cars_nearby_prompt);
                String promptS = String.format(prompt, obj);
                carNearbyTv.setText(promptS);
            }
        };
        threadhandler.startThread(null);
    }

    protected void initLocation() {
        AMapLocationListener listener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation != null && aMapLocation.getAMapException().getErrorCode() == 0) {
                    //获取位置信息
                    geoLat = aMapLocation.getLatitude();
                    geoLng = aMapLocation.getLongitude();
                    getCarNearby(geoLat, geoLng);
                }
            }

            @Override
            public void onLocationChanged(Location location) {
                System.out.println("onLocationChanged");
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                System.out.println("onStatusChanged");
            }

            @Override
            public void onProviderEnabled(String provider) {
                System.out.println("onProviderEnabled");
            }

            @Override
            public void onProviderDisabled(String provider) {
                System.out.println("onProviderDisabled");
            }
        };
        LocationManagerTool.locationListen(mContext, listener);
    }

}
